package services;
import java.io.*;
import java.net.*;

import pd.SwissSMSClient;

public class Xtrazone implements SMSService {
	
	private static final String host = "https://www.swisscom-mobile.ch";
	private String authcookie = "";
	
	public boolean doLogin(String userid, String password){
		String postheader = 
			"POST /youth/youth_zone_home-de.aspx?login HTTP/1.1\r\n" +
			"Host: www.swisscom-mobile.ch\r\n" +
			"Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n" +
			"Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3\r\n" +
			"Accept-Encoding: deflate\r\n" +
			"Accept-Charset: utf-8;q=0.7,*;q=0.7\r\n" +
			"Keep-Alive: 300\r\n" +
			"Connection: close\r\n" +
			"Referer: https://www.swisscom-mobile.ch/selfreg/SelfRegistration?uri=/youth/youth_zone_home-de.aspx\r\n" +
			"Content-Type: application/x-www-form-urlencoded\r\n" +
			"Content-Length: ";
		
		String postbody = "";
		
		try{
			postbody = "isiwebuserid=" + URLEncoder.encode(userid, "UTF8") + "&isiwebpasswd=" + URLEncoder.encode(password, "UTF8") + 
				"&isiwebjavascript=No&isiwebappid=mobile&isiwebmethod=authenticate&isiweburi=%2Fyouth%2Fyouth_zone_home-de.aspx&isiwebargs=&login.x=24&login.y=7";
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return false;
		}
		
		String post = postheader + postbody.length() + "\r\n\r\n" + postbody;
		
		String response = SwissSMSClient.sendSSL(host, post);
		
		String strsetcookie = "Set-Cookie: Navajo=";
		int coopos = response.indexOf(strsetcookie);
		int locpos = response.indexOf("Location: ");
		int delpos = response.indexOf("\r\n\r\n");
		
		if(coopos != -1 && locpos != -1 && delpos != -1 && coopos < delpos && locpos < delpos){
			authcookie = response.substring(coopos + strsetcookie.length(), delpos);
			authcookie = authcookie.substring(0, authcookie.indexOf(';'));	
			return true;
		}
		
		return false;
	}
	
	public boolean sendMessage(String number, String message){
		
		String postheaderp1 = 
			"POST /youth/sms_senden-de.aspx HTTP/1.1\r\n" +
			"Host: www.swisscom-mobile.ch\r\n" +
			"Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n" +
			"Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3\r\n" +
			"Accept-Encoding: deflate\r\n" +
			"Accept-Charset: utf-8;q=0.7,*;q=0.7\r\n" +
			"Keep-Alive: 300\r\n" +
			"Connection: close\r\n" +
			"Referer: https://www.swisscom-mobile.ch/youth/sms_senden-de.aspx\r\n" +
			"Cookie: Navajo=";
		
		String postheaderp2 = "; CP=null*; TreeView=00000000; Filename=../youth/sms_senden-de.aspx\r\n" +
			"Content-Type: application/x-www-form-urlencoded\r\n" +
			"Content-Length: ";
		
		String postbodyp1 = "__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE_SCM=2&__VIEWSTATE=&CobYouthSMSSenden%3AtxtMessage=";
		String postbodyp2 = "&CobYouthSMSSenden%3AtxtNewReceiver=";
		String postbodyp3 = "&CobYouthSMSSenden%3AbtnSend=Senden&FooterControl%3AhidNavigationName=SMS+senden&FooterControl%3AhidMailToFriendUrl=youth%2Fsms_senden-de.aspx";
		String postbody;
		try {
			postbody = postbodyp1 + URLEncoder.encode(message, "UTF8") + postbodyp2 + URLEncoder.encode(number, "UTF8") + postbodyp3;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return false;
		}
		
		String post = postheaderp1 + authcookie + postheaderp2 + postbody.length() + "\r\n\r\n" + postbody;
		
		SwissSMSClient.sendSSL(host, post);	
		return true;
	}

}
